package com.yyge.controller.mobile.product;


import cn.hutool.json.JSONUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.yyge.common.controller.BaseController;
import com.yyge.common.vo.resp.R;
import com.yyge.controller.admin.product.dto.DishFlavorItem;
import com.yyge.controller.admin.product.dto.DishFlavorProperty;
import com.yyge.controller.mobile.mapstruct.MobileCartConvert;
import com.yyge.controller.mobile.mapstruct.ProductConvert;
import com.yyge.controller.mobile.product.resp.MobileProductResp;
import com.yyge.entity.DishFlavor;
import com.yyge.entity.Product;
import com.yyge.service.DishFlavorService;
import com.yyge.service.ProductService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;
import java.util.ArrayList;
import java.util.List;


@Api(tags = "手机端-商品信息")
@RequestMapping(value = "/mobile/product")
@RestController
public class MobileProductController extends BaseController<Product,Object> {

    public MobileProductController(){
        this.converter=MobileCartConvert.class;
    }

    @Autowired
    private ProductService productService;

    @Autowired
    private DishFlavorService dishFlavorService;

    @ApiOperation(value = "查询")
    @GetMapping(value = "/list")
    public R<List<MobileProductResp>> getProduct(@Valid com.yyge.controller.mobile.product.req.MobileProductReq req){
         //http://localhost:8080/mobile/product/list?categoryId=48&merchantId=5 404 (OK)
        System.out.println("有没有映射Get");
        System.out.println(req);
        Product convert = ProductConvert.MAPPER.convert(req);
        convert.setIsgrounding("1");
        QueryWrapper<Product> productWrapper=new QueryWrapper<>(convert);

        List<Product> list = productService.list(productWrapper);
        System.out.println("list查询结果.....");
        System.out.println(list);
        List<MobileProductResp> resps=new ArrayList<>();

        //查询口味明细
        list.stream().forEach(p-> {
            //查询口味信息
            LambdaQueryWrapper<DishFlavor> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper.eq(DishFlavor::getDishId, p.getId());
            queryWrapper.eq(DishFlavor::getIsDeleted, "0");
            List<DishFlavor> dishFlavorList = dishFlavorService.list(queryWrapper);

            //组装成自己定义的口味数据给前端
            List<DishFlavorProperty> propertyList = new ArrayList<>();

            dishFlavorList.stream().forEach(s -> {
                DishFlavorProperty property = new DishFlavorProperty();
                property.setName(s.getName());
                //json反序列化
                List<DishFlavorItem> dishFlavorItems = JSONUtil.toList(s.getValue(), DishFlavorItem.class);
                property.setValue(dishFlavorItems);
                propertyList.add(property);
            });
            MobileProductResp mobileProductResp = new MobileProductResp();
            mobileProductResp.setId(p.getId());
            mobileProductResp.setDescription(p.getDescription());
            mobileProductResp.setMerchantId(p.getMerchantId());
            mobileProductResp.setScore(p.getScore());
            mobileProductResp.setDishFlavors(propertyList);
            mobileProductResp.setPicture(p.getPicture());
            mobileProductResp.setPrice(p.getPrice());
            mobileProductResp.setNum(p.getNum());
            mobileProductResp.setName(p.getName());
            resps.add(mobileProductResp);

        });
        System.out.println("查询结果");
        System.out.println(resps);
         return R.okHasData(resps);
    }

}
